﻿package com.dcliment.utils
{
	
	import flash.display.*;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	/**
	 * NOTE 11/18/2013 :: added gradient and if statement for border on circle
	 * NOTE 01/14/2014 :: added line
	 * 
	 * @author dcliment
	 * version 1.4
	 */	
	public class GraphicUtil 
	{				
		
		/**
		 * 
		 * Creates a rectangular Sprite
		 * 
		 * @param _left
		 * @param _top
		 * @param _right
		 * @param _bottom
		 * @param _color
		 * 
		 * @return Sprite
		 * 
		 */		
		public static function makeRectangle(_left:Number, _top:Number, _right:Number, _bottom:Number, _color:Number=0x00FF00, _alpha:Number = 1, _borderThickness:Number = 0, _borderColor:Number = 0x000000, _borderAlpha:Number = 0):Sprite
		{
			var _rect:Sprite = new Sprite();
				_rect.graphics.lineStyle(_borderThickness, _borderColor, _borderAlpha, true);
				_rect.graphics.beginFill(_color, _alpha);
				_rect.graphics.drawRect(_left, _top, _right, _bottom);
				_rect.graphics.endFill();
			
			return _rect;
		}
		// SOMETIMES YOU NEED IT AS A MOVIECLIP....
		public static function makeRectangleAsMovieClip(_left:Number, _top:Number, _right:Number, _bottom:Number, _color:Number=0x00FF00, _alpha:Number = 1):MovieClip
		{
			var _rect:MovieClip = new MovieClip();
				_rect.graphics.beginFill(_color, _alpha);
				_rect.graphics.drawRect(_left, _top, _right, _bottom);
				_rect.graphics.endFill();
				
			return _rect;
		}
		/**
		 * CREATES Rectangel with rounded edges
		 *  
		 * @param $left
		 * @param $top
		 * @param $right
		 * @param $bottom
		 * @param $curvature :: size of curvature on the edges
		 * @param $color
		 * @param $alpha
		 * @param $borderThickness
		 * @param $borderColor
		 * @return 
		 * 
		 */		
		public static function makeRoundRectangle($left:Number, $top:Number, $right:Number, $bottom:Number, $curvature:Number, $color:Number=0x00FF00, $alpha:Number = 1, $borderThickness:Number = 0, $borderColor:Number = 0x000000, $borderAlpha:Number = 0):Sprite
		{
			var rndRect:Sprite = new Sprite();
				rndRect.graphics.lineStyle($borderThickness, $borderColor, $borderAlpha, true);
				rndRect.graphics.beginFill($color, $alpha);
				rndRect.graphics.drawRoundRect($left, $top, $right, $bottom, $curvature);
				rndRect.graphics.endFill();
			
			return rndRect;
		}
		
		public static function makeCircle($x:Number, $y:Number, $radius:Number, $color:uint, $alpha:Number, $borderThickness:Number = 0, $borderColor:uint = 0x000000):Sprite
		{
			var circle:Sprite = new Sprite(); 
				circle.graphics.beginFill($color, $alpha); 
				if($borderThickness > 0)
					circle.graphics.lineStyle($borderThickness, $borderColor); 
				circle.graphics.drawCircle($x, $y, $radius); // Draw the circle, assigning it a x position, y position, raidius.
				circle.graphics.endFill(); 
			
			return circle;
			
		}
		public static function makeEllipse($x:Number, $y:Number, $w:Number, $h:Number, $color:uint, $alpha:Number, $borderThickness:Number = 0, $borderColor:uint = 0x000000):Sprite
		{
			var ellipse:Sprite = new Sprite(); 
				ellipse.graphics.beginFill($color, $alpha);
				if($borderThickness > 0)
					ellipse.graphics.lineStyle($borderThickness, $borderColor) 
				ellipse.graphics.drawEllipse($x, $y, $w, $h); // Draw the ellipse, assigning it a x position, y position, width and height.
				ellipse.graphics.endFill();
			
			return ellipse;
		}
		
		/**
		 * 
		 * Creates a Drop shawdow out of any displayObject
		 * 
		 * @param item Any DisplayObject
		 * @param _color
		 * @param _alpha
		 * @param _distance
		 * @param _blur
		 * @param _angle
		 * @return 
		 * 
		 */		
		public static function makeDropShadow(item:*, _color:uint = 0x000000, _alpha:Number = 0.5, _distance:Number = 1, _blur:Number = 1, _angle:Number = 45 ):void
		{
			
			var ds:DropShadowFilter = new DropShadowFilter();
				ds.color 		= _color;
				ds.blurX 		= _blur;
				ds.blurY 		= _blur;
				ds.angle 		= _angle;
				ds.alpha 		= _alpha;
				ds.distance 	= _distance;
			
			var filtersArray:Array = new Array(ds);
			item.filters = filtersArray;
		}
		
		/**
		 * The way to use it set your displayObjects.filters = "returned array"
		 * 
		 * @param _color
		 * @param _alpha
		 * @param _distance
		 * @param _blur
		 * @param _angle
		 * @return Array of filters
		 * 
		 */		
		public static function makeDropShadowFilter(_color:uint = 0x000000, _alpha:Number = 0.5, _distance:Number = 1, _blur:Number = 1, _angle:Number = 45 ):Array
		{
			
			var ds:DropShadowFilter = new DropShadowFilter();
				ds.color 		= _color;
				ds.blurX 		= _blur;
				ds.blurY 		= _blur;
				ds.angle 		= _angle;
				ds.alpha 		= _alpha;
				ds.distance 	= _distance;
			
			var filtersArray:Array = new Array(ds);
			
			return filtersArray;
		}
		
		/**
		 * Creates a mask aground the supplied MovieClip
		 * 
		 * @param The MovieClip to be masked
		 * @param width of the mask
		 * @param height of the mask
		 * @param x position
		 * @param y position
		 * 
		 * NOTES :: 01/29/2014 :: this does not work if the piece if resized. I think it might have to be added to the stage or something.. not sure what's the deal. -- Maybe get its parent and add it.. i dunno
		 * 
		 */			
		public static function makeMask($theMC:DisplayObject, $w:Number, $h:Number, $x:Number=0, $y:Number=0):void 
		{
			var _msk:Sprite = new Sprite();
				_msk.graphics.beginFill(0xcccccc);
				_msk.graphics.drawRect($x, $y, $w, $h);
				_msk.graphics.endFill();
			
			$theMC.mask=_msk;
		}
		// just added
		public static function makeGradientBox($w:Number, $h:Number, $color1:uint, $color2:uint, $angle:Number = 90):Sprite
		{
			//================================
			// BKGD Sprite
			//================================
			var bkgd:Sprite = new Sprite();
			//Type of Gradient we will be using
			var fType:String = GradientType.LINEAR;
			
			//Colors of our gradient in the form of an array
			var colors:Array = [$color1, $color2 ];
			
			//Store the Alpha Values in the form of an array
			var alphas:Array = [ 1, 1 ];
			
			//Array of color distribution ratios.  
			//The value defines percentage of the width where the color is sampled at 100%
			var ratios:Array = [ 0, 255 ];
			
			//Create a Matrix instance and assign the Gradient Box
			var matr:Matrix = new Matrix();
			matr.createGradientBox($w, $h, $angle, 0, 0);
			
			
			//SpreadMethod will define how the gradient is spread. Note!!! Flash uses CONSTANTS to represent String literals
			var sprMethod:String = SpreadMethod.PAD;			
			
			//Save typing + increase performance through local reference to a Graphics object
			var g:Graphics = bkgd.graphics;
			g.beginGradientFill( fType, colors, alphas, ratios, matr, sprMethod );
			g.drawRect(0, 0, $w, $h);
			
			return bkgd;
		}
		/**
		 * RETURNS a line
		 *  
		 * @param $x
		 * @param $y
		 * @param $borderThickness
		 * @param $borderColor
		 * @param $borderAlpha
		 * @return 
		 * 
		 */		
		public static function makeLine($x:Number, $y:Number, $borderThickness:Number = 1, $borderColor:Number = 0x000000, $borderAlpha:Number = 1):Sprite
		{
			var line:Sprite = new Sprite();
				line.graphics.lineStyle($borderThickness, $borderColor, $borderAlpha, true);
				line.graphics.lineTo($x, $y);
			
			return line;
		}
		
	}
}
